package com.yjt.fresh.widget;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.yjt.fresh.MyApp;
import com.yjt.fresh.R;
import com.yjt.fresh.utils.CommonsToolsHelper;

/**
 * 自定义Toast
 * Created by mcc on 2018-07-17.
 */

public class CustomToast {
    /**
     * 展示toast==LENGTH_SHORT
     *
     * @param msg
     */
    public static void show(String msg) {
        show(msg, Toast.LENGTH_SHORT);
    }

    /**
     * 展示toast==LENGTH_LONG
     *
     * @param msg
     */
    public static void showLong(String msg) {
        show(msg, Toast.LENGTH_LONG);
    }


    private static void show(String massage, int show_length) {
        Context context = MyApp.getContext();
        //使用布局加载器，将编写的toast_layout布局加载进来
        View view = View.inflate(context, R.layout.app_toast, null);
        LinearLayout layout = view.findViewById(R.id.llAppToast);
        TextView textView = view.findViewById(R.id.tvAppToast);
        textView.setText(massage);
        Toast toast = new Toast(context);
        //设置Toast要显示的位置，水平居中并在底部，X轴偏移0个单位，Y轴偏移70个单位，
        toast.setGravity(Gravity.TOP, 0, 0);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(CommonsToolsHelper.getWindowWidth(context),
                CommonsToolsHelper.dip2px(context,40));
        layout.setLayoutParams(layoutParams);
        //设置显示时间
        toast.setDuration(show_length);

        toast.setView(view);
        toast.show();
    }
}
